package cn.glinks.liuhe.widget.timeline;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.github.vipulasri.timelineview.TimelineView;

import java.util.List;

import cn.glinks.liuhe.R;
import cn.glinks.liuhe.activity.HistoryResultActivity;
import cn.glinks.liuhe.app.AppConstant;
import cn.glinks.liuhe.utils.ActTransAnimUtils;


public class TimeLineAdapter extends RecyclerView.Adapter<TimeLineViewHolder> {
    public final static int TYPE_PERSON = 1;
    public final static int TYPE_GROUP = 2;

    private List<TimeLineModel> mFeedList;
    private int mType = TYPE_PERSON;
    private Context mContext;
    private LayoutInflater mLayoutInflater;

    public TimeLineAdapter(List<TimeLineModel> feedList) {
        mFeedList = feedList;
    }

    public TimeLineAdapter(List<TimeLineModel> feedList, int type) {
        mFeedList = feedList;
        mType = type;
    }

    @Override
    public int getItemViewType(int position) {
        return TimelineView.getTimeLineViewType(position, getItemCount());
    }

    @Override
    public TimeLineViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        mContext = parent.getContext();
        mLayoutInflater = LayoutInflater.from(mContext);
        View view = mLayoutInflater.inflate(R.layout.item_timeline, parent, false);
        return new TimeLineViewHolder(view, viewType);
    }

    @Override
    public void onBindViewHolder(TimeLineViewHolder holder, int position) {

        final TimeLineModel timeLineModel = mFeedList.get(position);

        if (position == 0) {
            holder.mYearTextView.setText(timeLineModel.getYear());
            holder.mYearTextView.setVisibility(View.VISIBLE);
        } else {
            String prevYear = mFeedList.get(position-1).getYear();
            if (timeLineModel.getYear().equals(prevYear)) {
                holder.mYearTextView.setText(timeLineModel.getYear());
                holder.mYearTextView.setVisibility(View.INVISIBLE);
            } else {
                holder.mYearTextView.setText(timeLineModel.getYear());
                holder.mYearTextView.setVisibility(View.VISIBLE);
            }
        }

        holder.mDateTextView.setText(timeLineModel.getTimeStr());

        if (mType == TYPE_GROUP) {
            holder.mResultTextView.setText("测评报告");
        }

        holder.mResultButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 打包数据,传递并跳转到查看测评结果页面
                Bundle bundle = new Bundle();
                bundle.putSerializable(AppConstant.BUNDLE_HISTORY_COMMENT, timeLineModel);
                bundle.putInt(AppConstant.BUNDLE_HISTORY_TYPE, mType);
                ActTransAnimUtils.startActionSlideRightToLeft((Activity) mContext, HistoryResultActivity.class, bundle);
            }
        });
    }

    @Override
    public int getItemCount() {
        return (mFeedList != null ? mFeedList.size() : 0);
    }

}
